#!/usr/bin/env python

import sys, getopt, switch

def usage():
	print "usage: create_vlan [-h] [-a address] --vlan number --name string"
	print "\t-a or --address\t specify the IP address of the switch"
	print "\t-h\t\t display this help"
	print "\t--name\t\t the vlan name"
	print "\t--vlan\t\t the vlan number"
	print

def run_create_vlan(list_switchs, vlan, vlan_name):
	for ip in list_switchs:
		print "Connecting to " + ip + "..."
		tn = switch.connect(ip)
		if not tn:
			print "Error while connecting to " + ip
			continue
		name = switch.enable(tn)
		if not name:
			print "Error while connecting to " + ip
			continue
		print "Connected on " + name
		print "Creating the VLAN number " + vlan + " called " + vlan_name
		# TODO modify the output
		print switch.create_vlan(tn, number, vlan_name)
		switch.quit(tn)
		print

def main(argv):
	try:
		opts, args = getopt.getopt(argv[1:], "a:h", ["help", "address=", "vlan=", "name="])
	except getopt.GetoptError:
		usage()
		sys.exit(2)
	list_switchs = []
	vlan = ""
	name = ""
	for opt, arg in opts:
		if opt in ("-h", "--help"):
			usage()
			sys.exit()
		elif opt in ("-a", "--address"):
			list_switchs.append(arg)
		elif opt in "--vlan":
			vlan = arg
		elif opt in "--name":
			name = arg
	if vlan == "" or name == "":
		usage()
		sys.exit(2)
	if list_switchs == []:
		list_switchs = switch.get_switch_list()
	run_create_vlan(list_switchs, vlan, name)

if __name__ == "__main__":
	main(sys.argv)
